# -*- coding: utf-8 -*-

from server.controller import equipment
from server.controller import session
from server.controller import request
from server.controller import equipmentDao
from server.controller import datetime
from server.controller import json

equipmentObj = equipmentDao()


@equipment.route('/getEquipments')
def get_equipments():
    response = {}
    data = {}

    userSession = session.get('user')
    if userSession is None:
        response['code'] = 1
        response['msg'] = "用户未登录"
        response['data'] = data
        return response

    condition = {"effective": int(request.args.get('effective')),
                 "name": request.args.get('name'),
                 "type": request.args.get('type')}

    if condition['name'] == '' or condition['name'] is None:
        condition.pop('name')

    if condition['type'] == '0' or condition['type'] is None:
        condition.pop('type')

    limit = {"page": int(request.args.get('page')),
             "limit": int(request.args.get('limit'))}

    count, data = equipmentObj.select_equipments(condition, limit)

    if count == 0:
        response['code'] = 1
        response['msg'] = "无数据"
        response['data'] = data
        return response

    response['code'] = 0
    response['msg'] = "查询成功"
    response['count'] = count
    response['data'] = data

    return response


@equipment.route('/addEquipment', methods=['POST'])
def add_equipment():
    response = {}
    data = {}

    userSession = session.get('user')
    if userSession is None:
        response['code'] = 1
        response['msg'] = "用户未登录"
        response['data'] = data
        return response

    if userSession['role'] != '1' and userSession['role'] != '2':
        response['code'] = 1
        response['msg'] = "用户权限不足"
        response['data'] = data
        return response

    equipmentInfo = {"name": request.form.get('name'),
                     "producer": request.form.get('producer'),
                     "effective": 1,
                     "type": request.form.get('type'),
                     "createDate": datetime.datetime.now().strftime('%Y-%m-%d'),
                     "creator": userSession['username'],
                     "details": json.loads(request.form.get('details')),
                     "describe": request.form.get('describe')}

    if equipmentInfo['name'] == "" or equipmentInfo['type'] == "0":
        response['code'] = 1
        response['msg'] = "新建设备需填写设备名称和设备类型"
        response['data'] = data
        return response

    ret = equipmentObj.add_equipment(equipmentInfo)

    if not ret:
        response['code'] = 1
        response['msg'] = "新增失败"
        response['data'] = {}
        return response

    response['code'] = 0
    response['msg'] = "新增成功"
    response['data'] = data

    return response


@equipment.route('/updateEquipment', methods=['POST'])
def update_equipment():
    response = {}
    data = {}

    userSession = session.get('user')
    if userSession is None:
        response['code'] = 1
        response['msg'] = "用户未登录"
        response['data'] = data
        return response

    if userSession['role'] != '1' and userSession['role'] != '2':
        response['code'] = 1
        response['msg'] = "用户权限不足"
        response['data'] = data
        return response

    equipmentInfo = {"id": request.form.get('id'),
                     "name": request.form.get('name'),
                     "producer": request.form.get('producer'),
                     "type": request.form.get('type'),
                     "createDate": datetime.datetime.now().strftime('%Y-%m-%d'),
                     "creator": userSession['username'],
                     "details": json.loads(request.form.get('details')),
                     "describe": request.form.get('describe')}

    ret = equipmentObj.update_equipment(equipmentInfo)

    if not ret:
        response['code'] = 1
        response['msg'] = "修改失败"
        response['data'] = {}
        return response

    response['code'] = 0
    response['msg'] = "修改成功"
    response['data'] = data

    return response


@equipment.route('/invalidEquipment', methods=['POST'])
def invalid_equipment():
    response = {}
    data = {}

    userSession = session.get('user')
    if userSession is None:
        response['code'] = 1
        response['msg'] = "用户未登录"
        response['data'] = data
        return response

    if userSession['role'] != '1' and userSession['role'] != '2':
        response['code'] = 1
        response['msg'] = "用户权限不足"
        response['data'] = data
        return response

    ret = True
    equipments = request.form.get('invalidData')
    if not equipments:
        response['code'] = 1
        response['msg'] = "传入参数异常"
        response['data'] = data
        return response

    for strId in json.loads(equipments):
        if not equipmentObj.invalid_equipment({"id": strId}):
            data.append({"id": strId})
            ret = False

    if not ret:
        response['code'] = 1
        response['msg'] = "设置设备失效失败"
        response['data'] = data
        return response

    response['code'] = 0
    response['msg'] = "设置设备失效成功"
    response['data'] = data

    return response


@equipment.route('/activateEquipment', methods=['POST'])
def activate_equipment():
    response = {}
    data = {}

    userSession = session.get('user')
    if userSession is None:
        response['code'] = 1
        response['msg'] = "用户未登录"
        response['data'] = data
        return response

    if userSession['role'] != '1' and userSession['role'] != '2':
        response['code'] = 1
        response['msg'] = "用户权限不足"
        response['data'] = data
        return response

    ret = True
    equipments = request.form.get('activateData')
    for strId in json.loads(equipments):
        if not equipmentObj.activate_equipment({"id": strId}):
            data.append({"id": strId})
            ret = False

    if not ret:
        response['code'] = 1
        response['msg'] = "设置设备激活失败"
        response['data'] = data
        return response

    response['code'] = 0
    response['msg'] = "设置设备激活成功"
    response['data'] = data

    return response


@equipment.route('/deleteEquipment', methods=['POST'])
def delete_equipment():
    response = {}
    data = {}

    userSession = session.get('user')
    if userSession is None:
        response['code'] = 1
        response['msg'] = "用户未登录"
        response['data'] = data
        return response

    if userSession['role'] != '1' and userSession['role'] != '2':
        response['code'] = 1
        response['msg'] = "用户权限不足"
        response['data'] = data
        return response

    ret = True
    equipments = request.form.get('batchData')
    for strId in json.loads(equipments):
        if not equipmentObj.delete_equipment({"id": strId}):
            data.append({"id": strId})
            ret = False

    if not ret:
        response['code'] = 1
        response['msg'] = "删除设备失败"
        response['data'] = data
        return response

    response['code'] = 0
    response['msg'] = "删除设备成功"
    response['data'] = data

    return response
